package com.example.demo.controller;

import com.example.demo.model.UserInfo;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: fzykd
 * Date: 2022-10-15
 * Time: 18:22
 */


@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    //获取一个或多个参数
    @RequestMapping("/login")
    public int login(Integer id){
        return id;
    }

    //获取一个对象
    @RequestMapping("/info")
    public String userInfo(UserInfo userInfo){
        return "" + userInfo;
    }
    // 如果方法的返回值是String 返回的是这样的 字符串UserInfo(id=10, name=张三, password=123456, age=0)
    // 如果方法的返回值是UserInfo对象 那么返回值是Json格式的对象


    //获取Json格式的数据
    @RequestMapping("/json")
    //接受前端传来Json格式的数据
    public UserInfo userInfo1(@RequestBody(required = false) UserInfo userInfo){
        return userInfo;
    }
    //没加注解@RequestBody 得到的是空字符串
    //加了注解 没传参数 报错
    //加了注解 并且加了required = false 没传参数不报错  且能接收Json格式的数据

    //获取url地址中的参数
    @RequestMapping("/url/{id}/{name}")
    public String url(@PathVariable Integer id,@PathVariable String name){
        return "id: " + id + "| name: " + name;
    }

    //获取Cookie
    //servlet 获取 Cookie的方法
    @RequestMapping("/coookie")
    public void getCookie(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        for (Cookie item : cookies) {
            log.info("Cookie Name " + item.getName() +
                    " | Cookie Value: " + item.getValue());
        }
    }

    //通过注解获取Cookie
    @RequestMapping("/cookie")
    public String getC(@CookieValue("hello") String cookie){
        return "Cookie Value: " + cookie;
    }

    //获取Header里面的信息
    @RequestMapping("/getua")
    public String getHead(HttpServletRequest request){
        return "UA :" + request.getHeader("User-Agent");
    }

    //通过注解来获取信息
    @RequestMapping("/getua2")
    public String getUA(@RequestHeader("User-Agent") String head){
        return "hea: " + head;
    }

    //存储Session 只有这一种办法
    @RequestMapping("/ses")
    public boolean session(HttpServletRequest request){
        boolean result = false;
        //得到session对象 HttpSession
        HttpSession session = request.getSession(true);
        //2.使用set设置值
        session.setAttribute("userinfo","userinfo");
        result = true;
        return result;
    }

    //获取Session
    //只有先创建了 才能获取
    @RequestMapping("/getses")
    public String getSession(@SessionAttribute(value = "userinfo",required = false) String userinfo){
        return "会话: " + userinfo;
    }

    //servlet的方式 获取Session
    @RequestMapping("/session")
    public String session1(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        //要进行非空判断 没有机会报错
        return "会话" + session.getAttribute("userinfo");
    }

}
